package com.example.security.demo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.security.demo.dto.UserDto;
import com.example.security.demo.entity.SysMenu;
import com.example.security.demo.entity.SysUser;
import com.example.security.demo.mapper.SysUserMapper;
import lombok.AllArgsConstructor;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhanpengguo
 * @date 2019-10-08 16:34
 */
@Service
@AllArgsConstructor
public class UserDetailsServiceImpl implements UserDetailsService {

    private final SysUserMapper sysUserMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUser::getUsername, username);
        SysUser sysUser = sysUserMapper.selectOne(queryWrapper);
        UserDto userDto = new UserDto();
        BeanUtil.copyProperties(sysUser,userDto);
        List<SysMenu> sysMenus = sysUserMapper.selectMenuByUsername(username);
        userDto.setSysMenus(sysMenus);
        return userDto;
    }
}
